﻿using BarcodeDelay;
using JTParcrelHelper.bean;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JTParcrelHelper.view
{
    public partial class FrmInboundSetting : Form
    {
        public FrmInboundSetting()
        {
            InitializeComponent();
        }

        Hashtable htFirstType = new Hashtable();
        Hashtable htSecondType = new Hashtable();
        ArrayList arListFirsType = new ArrayList();
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if(txtLeaveMsg.Text.Trim().Length<5)
                {
                    MessageBox.Show("留言模板不能为空");
                    return;
                }
                if(cbFirst.SelectedItem==null)
                {
                    MessageBox.Show("一级类型不能为空");
                    return;
                }
                StoParams.Instance.inboundTemplete = txtLeaveMsg.Text.Trim();
                
                if(htSecondType.ContainsKey(cbSecond.SelectedItem.ToString()))
                {
                    StoParams.Instance.secondProblemBean = htSecondType[cbSecond.SelectedItem.ToString()] as SignPrblemType;
                }
                else
                {
                    MessageBox.Show("二级类型不能为空");
                    return;
                }
                this.Close();

            }
            catch(Exception ex)
            {

            }
        }

        private void FrmInboundSetting_Load(object sender, EventArgs e)
        {
            try
            {
                getFirstType();
                initCbFirst();

            }
            catch (Exception ex)
            {

            }
        }
        private void initCbFirst()
        {
            try
            {
                foreach(SignPrblemType signBean in arListFirsType)
                {
                    cbFirst.Items.Add(signBean.name);
                }
            }
            catch(Exception x)
            {

            }
        }

        private  void getFirstType()
        {
            string strUrl = "https://jmsgw.jtexpress.com.cn/basicdata/sysAbnormalPiece/select?size=999999&levelType=1&operatorType=4";
            try
            {
                arListFirsType.Clear();
                JObject jObj = new JObject();

                string postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                string resMsg = BarcodeDelay.HttpClient.HttpUtil.Instance.QueryCommon(strUrl, postData, "GET", "batchProblem");
                if (!resMsg.Contains("请求成功"))
                {
                    return;
                }

                JObject jsonObjRes = (JObject)JsonConvert.DeserializeObject(resMsg);
                JObject data = (JObject)jsonObjRes["data"];
                JArray records = (JArray)data["records"];
                for (int i = 0; i < records.Count; i++)
                {
                    SignPrblemType signPrblem = new SignPrblemType();
                    JObject json = (JObject)records[i];
                    JToken id = json["id"];
                    JToken code = json["code"];
                    JToken name = json["name"];
                    if (id != null)
                    {
                        signPrblem.id = Convert.ToInt32(id);
                        signPrblem.name = name.ToString();
                        signPrblem.code = code.ToString();
                        htFirstType[signPrblem.name] = signPrblem;
                        StoParams.Instance.htFirstProblemType[signPrblem.name]= signPrblem;
                        arListFirsType.Add(signPrblem);
                    }
                }
            }
            catch(Exception ex)
            {

            }
        }

        private ArrayList getSecondType(string firstLevelID)
        {
            ArrayList arListType = new ArrayList();
            string strUrl = "https://jmsgw.jtexpress.com.cn/servicequality/comboBox/levelTwoTypeList?parentId="+ firstLevelID + "&opType=4";
            try
            {
                JObject jObj = new JObject();

                string postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                string resMsg = BarcodeDelay.HttpClient.HttpUtil.Instance.QueryCommon(strUrl, postData, "GET", "batchProblem");
                if (!resMsg.Contains("请求成功"))
                {
                    return arListType;
                }

                JObject jsonObjRes = (JObject)JsonConvert.DeserializeObject(resMsg);
                JArray records = (JArray)jsonObjRes["data"];
                for (int i = 0; i < records.Count; i++)
                {
                    SignPrblemType signPrblem = new SignPrblemType();
                    JObject json = (JObject)records[i];
                    JToken id = json["id"];
                    JToken code = json["code"];
                    JToken name = json["name"];
                    if (id != null)
                    {
                        signPrblem.id = Convert.ToInt32(id);
                        signPrblem.name = name.ToString();
                        signPrblem.code = code.ToString();
                        htSecondType[signPrblem.name] = signPrblem;
                        StoParams.Instance.htSecondProblemType[signPrblem.name] = signPrblem;
                        arListType.Add(signPrblem);


                    }
                }
            }
            catch (Exception ex)
            {

            }
            return arListType;
        }

        private void cbFirst_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string selectItem = cbFirst.SelectedItem.ToString();
                if(htFirstType.ContainsKey(selectItem))
                {
                    SignPrblemType signBean = htFirstType[selectItem] as SignPrblemType;
                    StoParams.Instance.firstProblemBean = signBean;
                    ArrayList arListSeconds = getSecondType(signBean.id.ToString());
                    cbSecond.Items.Clear();
                    cbSecond.Refresh();
                    foreach (SignPrblemType curBean in arListSeconds)
                    {
                        cbSecond.Items.Add(curBean.name);
                    }
                    cbSecond.SelectedIndex = 0;
                }

            }
            catch(Exception ex)
            {

            }
        }

        private void cbSecond_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                SignPrblemType signBean = new SignPrblemType();
                string selectItem = cbSecond.SelectedItem.ToString();
                if (htSecondType.ContainsKey(selectItem))
                {
                    signBean = htSecondType[selectItem] as SignPrblemType;
                }
                else
                {
                    return;
                }

                string strUrl = "https://jmsgw.jtexpress.com.cn/servicequality/problemPiece/checkProblemType?problemTypeCode=" + signBean.code;
                JObject jObj = new JObject();

                string postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                string resMsg = BarcodeDelay.HttpClient.HttpUtil.Instance.QueryCommon(strUrl, postData, "GET", "batchProblem");
        

                JObject jsonObjRes = (JObject)JsonConvert.DeserializeObject(resMsg);
                JObject dataObj = (JObject)jsonObjRes["data"];
                string reasonDesc = dataObj["reasonDesc"].ToString();
                this.txtLeaveMsg.Text = reasonDesc;
            }
            catch(Exception ex)
            {
                
            }
        }
    }
}
